package com.ghc.action;

import java.util.ArrayList;
import java.util.List;


import org.apache.struts2.ServletActionContext;

import com.ghc.dao.NoticeDao;
import com.ghc.pojo.Notice;
import com.ghc.util.PageUtil;
import com.ghc.vo.NoticeVo;
import com.google.gson.Gson;


public class NoticeAction {
	
	private Notice notice = new Notice();
	private NoticeDao noticeDao;
	@SuppressWarnings("rawtypes")
	private PageUtil vo = new PageUtil();
	ArrayList<Notice> noticeList = new ArrayList<Notice>();
	ArrayList<NoticeVo> noticeVList = new ArrayList<NoticeVo>();
	NoticeVo noticeVo = new NoticeVo();
	private String result;
	public Gson gson = new Gson();
	
	@SuppressWarnings("unchecked")
	public String showNoticeAll(){
		int page = 1;
		if(ServletActionContext.getRequest().getParameter("page")!=null&&ServletActionContext.getRequest().getParameter("page")!=""){
			if(Integer.parseInt(ServletActionContext.getRequest().getParameter("page"))>0){
				page = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
			}
		}else{
			page = 1;
		}
		int pageSize = 10;
		int allRow = noticeDao.getRow();
		int pageCount = PageUtil.countPage(pageSize, allRow);
		final int start = PageUtil.start(pageSize, page);// 当前页开始记录行号
		final int end = pageSize * page;// 每页结束记录行号
		final int pageCurrent = PageUtil.CurrentPage(page);
		notice.setStart(start);
		notice.setEnd(end);
		
		noticeList=noticeDao.showNoticeAll(notice);
		
		vo.setPageSize(pageSize);
		vo.setPageCurrent(pageCurrent);
		vo.setAllrow(allRow);
		vo.setPageCount(pageCount);
		vo.setList(noticeList);
		vo.init();
		
		return "success";
	}
	
	public String showNotice(){
		notice = noticeDao.showNotice(notice.getId());
		if(notice==null){
			return "no";
		}
		return "success";
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String showNoticeJS(){
		int allRow = noticeDao.getRow();
		noticeVList = noticeDao.showNoticeJS();
		List list = new ArrayList();
		list.add(allRow);
		list.add(noticeVList);
		
		result = new Gson().toJson(list);
		return "success";
	}
	
	public String addNotice(){
		try {
			noticeDao.AddNotice(notice);
			result = "got";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	public String showNoticeOne(){
		notice = noticeDao.showNoticeOne(notice.getId());
		result = new Gson().toJson(notice);
		return "success";
	}
	
	public String updateNotice(){
		try {
			noticeDao.updateNotice(notice);
			result = "edit";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	public String delNotice(){
		try {
			noticeDao.delNotice(notice.getId());
			result = "del";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}

	@SuppressWarnings("rawtypes")
	public PageUtil getVo() {
		return vo;
	}

	@SuppressWarnings("rawtypes")
	public void setVo(PageUtil vo) {
		this.vo = vo;
	}

	public NoticeDao getNoticeDao() {
		return noticeDao;
	}

	public void setNoticeDao(NoticeDao noticeDao) {
		this.noticeDao = noticeDao;
	}

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public ArrayList<Notice> getNoticeList() {
		return noticeList;
	}

	public void setNoticeList(ArrayList<Notice> noticeList) {
		this.noticeList = noticeList;
	}
	
	
	public ArrayList<NoticeVo> getNoticeVList() {
		return noticeVList;
	}

	public void setNoticeVList(ArrayList<NoticeVo> noticeVList) {
		this.noticeVList = noticeVList;
	}

	public NoticeVo getNoticeVo() {
		return noticeVo;
	}

	public void setNoticeVo(NoticeVo noticeVo) {
		this.noticeVo = noticeVo;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
}
